#include "scanui.h"
#include "ui_scanui.h"
#include "constants.h"
#include "infoui.h"
#include "log4z.h"
#include "paramui.h"
#include "rs232.h"
#include <QTimer>
#include <QDateTime>
#include <QTextCodec>

ScanUI::ScanUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScanUI),
    bStayScan(false),
    scanIdx(0)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
//    QPalette palette = ui->mainPnl->palette();
//    palette.setColor(backgroundRole(), QColor(255,255,255));
//    ui->mainPnl->setPalette(palette);
//    ui->mainPnl->setAutoFillBackground(true);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->title->setPalette(pe);

    QPixmap scnImg(swStr);
    ui->icnOne->setPixmap(scnImg);
    ui->icnTwo->setPixmap(scnImg);
    ui->icnThree->setPixmap(scnImg);

    //button style
    ui->btnRet->setStyleSheet(blueBtn);
    QPalette btnPe;
    btnPe.setColor(QPalette::ButtonText,Qt::white);
    ui->btnRet->setPalette(btnPe);

    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);

    //init scanner
    int prt = 16;
    int bdrate=115200;       /* 57600 baud */
    char mode[] = {'8','N','1',0};
    if(RS232_OpenComport(prt, bdrate, mode))
    {
        LOGFMTE("Open scanner Failed!");
    }

    //create read thread
    BarcodeData* dcReadData = new BarcodeData();
    connect(dcReadData, SIGNAL(readData(const char*)), SLOT(onReadData(const char*)));
    dcReadData->start();
}

ScanUI::~ScanUI()
{
    delete ui;
    if (dcReadData)
    {
        dcReadData->stopRead();
        delete dcReadData;
        dcReadData = 0;
    }

    RS232_CloseComport(16);
}

void ScanUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void ScanUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
    scanIdx = 0;
    bStayScan = true;
    QPixmap scnImg(swStr);
    ui->icnOne->setPixmap(scnImg);
    ui->icnTwo->setPixmap(scnImg);
    ui->icnThree->setPixmap(scnImg);
}

void ScanUI::on_btnRet_clicked()
{
    bStayScan = false;
    this->hide();
    emit retMain();
}

void ScanUI::onReadData(const char* qData)
{
    if (!bStayScan)
        return;
    QTextCodec *cod = QTextCodec::codecForName("GB2312");
    QString qbar = cod->toUnicode(qData);
    QStringList qdetail = qbar.split("|");

    //ui->tlThree->setText(qbar);
    if (qdetail.size() == 10)
    {
        scanIdx++;
        LOGFMTD("Func %s: idx: %d", __func__, scanIdx);
        if (scanIdx == 1)
        {
            firstBarcode = qdetail[2];
            LOGFMTD("First barcode %s", firstBarcode.toStdString().c_str());
            QPixmap scnImg(sdStr);
            ui->icnOne->setPixmap(scnImg);
        } else if (scanIdx == 2)
        {
            secondBarcode = qdetail[2];
            LOGFMTD("Second barcode %s", secondBarcode.toStdString().c_str());
            if (secondBarcode.compare(firstBarcode) != 0)
            {
                //error reset
                secondBarcode = "";
                scanIdx = 0;
                QPixmap scnImg(swStr);
                ui->icnOne->setPixmap(scnImg);
                InfoUI ifu;
                ifu.InfoDialog();
                ifu.SetInfo(tr("Error"), tr("Barcode different!"));
                ifu.exec();
            } else
            {
                QPixmap scnImg(sdStr);
                ui->icnTwo->setPixmap(scnImg);
            }
        } else if (scanIdx == 3)
        {
            thirdBarcode = qdetail[2];
            LOGFMTD("Third barcode %s", thirdBarcode.toStdString().c_str());
            if (thirdBarcode.compare(firstBarcode) != 0)
            {
                //error reset
                secondBarcode = "";
                thirdBarcode = "";
                scanIdx = 0;
                QPixmap scnImg(swStr);
                ui->icnOne->setPixmap(scnImg);
                ui->icnTwo->setPixmap(scnImg);
                InfoUI ifu;
                ifu.InfoDialog();
                ifu.SetInfo(tr("Error"), tr("Barcode different!"));
                ifu.exec();
                return;
            }
            QPixmap scnImg(sdStr);
            ui->icnThree->setPixmap(scnImg);
            //assign all data
            ParamUI::qstrName = qdetail[0];
            ParamUI::qstrId = qdetail[1];
            ParamUI::qstrBarcode = qdetail[2];
            ParamUI::qstrCell = qdetail[3];
            ParamUI::qstrDianya = qdetail[4];
            ParamUI::qstrMaikuan = qdetail[5];
            ParamUI::qstrCishu = qdetail[6];
            ParamUI::qstrJiange = qdetail[7];
            ParamUI::qstrLiusu = qdetail[8];

            LOGFMTD("Last barcode %s", ParamUI::qstrBarcode.toStdString().c_str());
            if (QString::compare(qdetail[9], "0") == 0)
            {
                ParamUI::blYaodong = false;
            } else if (QString::compare(qdetail[9], "1") == 0)
            {
                ParamUI::blYaodong = true;
            }

            this->hide();
            emit paramSet(1);
            scanIdx = 0;
            bStayScan = false;
        }
//        if (qdetail.at(9) == 'a')
//        {
//            //first
//        }
//        else if (qdetail.at(9) == 'b')
//        {
//            //second
//        }
//        else
//        {
//            //third
//            //goto set ui
//        }

    }
}

